package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.common.utils.JsonUtils;
import com.redoor.app.stake.workflow.model.BaseWorkorderOperationLog;
import com.redoor.app.stake.workflow.service.WorkOrderLogService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangjunlei on 2018/2/23.
 */

@Service
public class WorkOrderLogServiceImpl implements WorkOrderLogService {

    @Autowired
    RedisUtils redisUtils;


    @Override
    public boolean addWorOrderLogToRedis(Object obj,LogEnum logEnum) {
        if(null != logEnum){
            return redisUtils.push(logEnum.getName(), JsonUtils.toJson(obj)) > 0;
        } else {
            return false;
        }
    }

    public Object getWorkOrderLogFromRedis(LogEnum logEnum) {
        if(null == logEnum){
            return null;
        }
        long size = redisUtils.lGetListSize(logEnum.getName());
        if(size > 0) {
            if(logEnum == LogEnum.WORKORDER_LIFE_LOG){
                return JsonUtils.readValue(redisUtils.pop(logEnum.getName()).toString(), BaseWorkorderOperationLog.class);
            }
        }
        return null;
    }

    @Override
    public List getWorkOrderLogFromRedis(int times,LogEnum logEnum) {
        List logList = new ArrayList();
        if (times < 1) {
            times = 1;
        }
        if(null == logEnum){
            return logList;
        }
        while (logList.size() < times){
            Object obj = getWorkOrderLogFromRedis(logEnum);
            if(null != obj){
                logList.add(obj);
            } else {
                break;
            }
        }
        return logList;
    }
}
